package com.distributeredis.redis_springboot.springeasyexcel.writetest.write;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.distributeredis.redis_springboot.springeasyexcel.vo.User;
import org.joda.time.DateTime;
import org.junit.Test;

import java.util.*;

public class WriteTest {
    String PATH = "H:\\LUA\\redis_springboot\\src\\main\\java\\com\\distributeredis\\redis_springboot\\springeasyexcel\\writetest\\";
    // 方式一
    @Test
    public void test1() {
        String fileName = "user1.xlsx";
        List<User> users = Arrays.asList(
                new User(1001, "李雷", "男", 100.110, new Date()),
                new User(1002, "张三", "男", 110.210, new Date()),
                new User(1003, "李四", "女", 120.240, new Date()),
                new User(1004, "王五", "女", 130.540, new Date()));
        // excel表格中写入数据
        EasyExcel.write(PATH+fileName, User.class).sheet("用户信息").doWrite(users);
    }

    // 排除模板中某些不必要字段
    @Test
    public void test3() {
        String fileName = "user3.xlsx";
        List<User> users = Arrays.asList(
                new User(1001, "李雷", "男", 100.0, new Date()),
                new User(1002, "张三", "男", 110.0, new Date()),
                new User(1003, "李四", "女", 120.0, new Date()),
                new User(1004, "王五", "女", 130.0, new Date()));

        Set<String> set = new HashSet<>();
        set.add("hireDate");
        set.add("salary");

        // excel表格中写入数据
        EasyExcel.write(PATH+fileName, User.class)
                .excludeColumnFieldNames(set)
                .sheet("用户信息")
                .doWrite(users);
    }

    /**
     * 指定·模板中的某几个属性写入到Excel中
     */
    @Test
    public void test4() {
        String fileName = "user4.xlsx";
        List<User> users = Arrays.asList(
                new User(1001, "李雷", "男", 100.0, new Date()),
                new User(1002, "张三", "男", 110.0, new Date()),
                new User(1003, "李四", "女", 120.0, new Date()),
                new User(1004, "王五", "女", 130.0, new Date()));

        Set<String> set = new HashSet<>();
        set.add("hireDate");
        set.add("salary");

        // excel表格中写入数据
        EasyExcel.write(PATH+fileName, User.class)
                .includeColumnFieldNames(set)
                .sheet("用户信息")
                .doWrite(users);
    }

    // 方式三
    @Test
    public void test2() {
        String fileName = "user2.xlsx";
        List<User> users = Arrays.asList(
                new User(1001, "李雷", "男", 100.0, new Date()),
                new User(1002, "张三", "男", 110.0, new Date()),
                new User(1003, "李四", "女", 120.0, new Date()),
                new User(1004, "王五", "女", 130.0, new Date()));
        // excel表格中写入数据
        ExcelWriter excelWriter = EasyExcel.write(PATH + fileName, User.class).build();
        WriteSheet writeSheet = EasyExcel.writerSheet("用户信息").build();
        excelWriter.write(users,writeSheet);
        excelWriter.finish();
    }
}
